package com.neusoft.controller;


import com.neusoft.po.Cart;
import com.neusoft.po.CommonResult;
import com.neusoft.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;
import sun.tools.jar.resources.jar;
import sun.util.logging.resources.logging;

import java.util.List;


@RestController
@RequestMapping("/CartController")
@RefreshScope
public class CartController {

    @Autowired
    private CartService cartService;

    //select * from cart where userId=#{userId} and businessId=#{businessId}
    @RequestMapping("/listCart")
    public CommonResult<List> listCart(Cart cart) throws Exception{
        return new CommonResult(200, "success", cartService.listCart(cart));
    }

    //插入一条数据，包含主键的三个属性和数量
    @PostMapping("/saveCart/{foodId}/{businessId}/{userId}")
    public CommonResult<Integer> saveCart(@PathVariable("foodId") Integer foodId,
                                          @PathVariable("businessId") Integer businessId,
                                          @PathVariable("userId") String userId
                                          ) throws Exception{
        return new CommonResult(200, "success", cartService.saveCart(foodId,businessId,userId));
    }

    //按照数量更新商家和用户之间的某个商品
    @PutMapping("/updateCart/{userId}/{businessId}/{foodId}/{quantity}")
    public CommonResult<Integer> updateCart(
            @PathVariable("userId") String userId,
            @PathVariable("businessId") Integer businessId,
            @PathVariable("foodId") Integer foodId,
            @PathVariable("quantity") Integer quantity) throws Exception {
        Cart param = new Cart();
        param.setUserId(userId);
        param.setBusinessId(businessId);
        param.setFoodId(foodId);
        param.setQuantity(quantity);
        int result = cartService.updateCart(param);
        return new CommonResult(200, "success", result);
    }

    @DeleteMapping("/removeCart/{userId}/{businessId}/{foodId}")
    public CommonResult<Integer> removeCart(
            @PathVariable("userId") String userId,
            @PathVariable("businessId") Integer businessId,
            @PathVariable("foodId") Integer foodId) throws Exception {
        Cart param = new Cart();
        param.setUserId(userId);
        param.setBusinessId(businessId);
        param.setFoodId(foodId);

        int result = cartService.removeCart(param);
        return new CommonResult(200, "success", result);
    }

    //清空这个用户和商家之间的购物车
    @PostMapping("/clearCart/{userId}/{businessId}")
    public CommonResult<Integer> clearCart(@PathVariable("userId") String userId,
                                           @PathVariable("businessId") Integer businessId) throws Exception{
        Cart cart=new Cart();
        cart.setBusinessId(businessId);
        cart.setUserId(userId);
        List<Cart> list = cartService.listCart(cart);
        for(Cart c:list){

            if(cartService.removeCart(c)==0){
                return new CommonResult(500, "fail", 0);
            }
        }
        return new CommonResult(200, "success", 1);
    }
}
